{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Candlestick Chart\n",
    "=================\n",
    "A candlestick chart inspired from Protovis(http://mbostock.github.io/protovis/ex/candlestick.html). This example shows the performance of the Chicago Board Options Exchange Volatility Index (VIX) in the summer of 2009. The thick bar represents the opening and closing prices, while the thin bar shows intraday high and low prices; if the index closed higher on a given day, the bars are colored green rather than red.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame(\n",
    "[\n",
    "      {\n",
    "        \"date\": \"01-Jun-2009\",\n",
    "        \"open\": 28.7,\n",
    "        \"high\": 30.05,\n",
    "        \"low\": 28.45,\n",
    "        \"close\": 30.04,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -4.89396411092985\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"02-Jun-2009\",\n",
    "        \"open\": 30.04,\n",
    "        \"high\": 30.13,\n",
    "        \"low\": 28.3,\n",
    "        \"close\": 29.63,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -0.322580645161295\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"03-Jun-2009\",\n",
    "        \"open\": 29.62,\n",
    "        \"high\": 31.79,\n",
    "        \"low\": 29.62,\n",
    "        \"close\": 31.02,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 3.68663594470045\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"04-Jun-2009\",\n",
    "        \"open\": 31.02,\n",
    "        \"high\": 31.02,\n",
    "        \"low\": 29.92,\n",
    "        \"close\": 30.18,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 4.51010886469673\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"05-Jun-2009\",\n",
    "        \"open\": 29.39,\n",
    "        \"high\": 30.81,\n",
    "        \"low\": 28.85,\n",
    "        \"close\": 29.62,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 6.08424336973478\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"08-Jun-2009\",\n",
    "        \"open\": 30.84,\n",
    "        \"high\": 31.82,\n",
    "        \"low\": 26.41,\n",
    "        \"close\": 29.77,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 1.2539184952978\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"09-Jun-2009\",\n",
    "        \"open\": 29.77,\n",
    "        \"high\": 29.77,\n",
    "        \"low\": 27.79,\n",
    "        \"close\": 28.27,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -5.02431118314424\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"10-Jun-2009\",\n",
    "        \"open\": 26.9,\n",
    "        \"high\": 29.74,\n",
    "        \"low\": 26.9,\n",
    "        \"close\": 28.46,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -5.46623794212217\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"11-Jun-2009\",\n",
    "        \"open\": 27.36,\n",
    "        \"high\": 28.11,\n",
    "        \"low\": 26.81,\n",
    "        \"close\": 28.11,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -8.3743842364532\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"12-Jun-2009\",\n",
    "        \"open\": 28.08,\n",
    "        \"high\": 28.5,\n",
    "        \"low\": 27.73,\n",
    "        \"close\": 28.15,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": -5.52763819095477\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"15-Jun-2009\",\n",
    "        \"open\": 29.7,\n",
    "        \"high\": 31.09,\n",
    "        \"low\": 29.64,\n",
    "        \"close\": 30.81,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": 3.4920634920635\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"16-Jun-2009\",\n",
    "        \"open\": 30.81,\n",
    "        \"high\": 32.75,\n",
    "        \"low\": 30.07,\n",
    "        \"close\": 32.68,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 0.155038759689914\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"17-Jun-2009\",\n",
    "        \"open\": 31.19,\n",
    "        \"high\": 32.77,\n",
    "        \"low\": 30.64,\n",
    "        \"close\": 31.54,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 5.82822085889571\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"18-Jun-2009\",\n",
    "        \"open\": 31.54,\n",
    "        \"high\": 31.54,\n",
    "        \"low\": 29.6,\n",
    "        \"close\": 30.03,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 8.17610062893082\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"19-Jun-2009\",\n",
    "        \"open\": 29.16,\n",
    "        \"high\": 29.32,\n",
    "        \"low\": 27.56,\n",
    "        \"close\": 27.99,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 8.59872611464968\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"22-Jun-2009\",\n",
    "        \"open\": 30.4,\n",
    "        \"high\": 32.05,\n",
    "        \"low\": 30.3,\n",
    "        \"close\": 31.17,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 15.4907975460123\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"23-Jun-2009\",\n",
    "        \"open\": 31.3,\n",
    "        \"high\": 31.54,\n",
    "        \"low\": 27.83,\n",
    "        \"close\": 30.58,\n",
    "        \"signal\": \"short\",\n",
    "        \"ret\": 11.7370892018779\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"24-Jun-2009\",\n",
    "        \"open\": 30.58,\n",
    "        \"high\": 30.58,\n",
    "        \"low\": 28.79,\n",
    "        \"close\": 29.05,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": -10.4234527687296\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"25-Jun-2009\",\n",
    "        \"open\": 29.45,\n",
    "        \"high\": 29.56,\n",
    "        \"low\": 26.3,\n",
    "        \"close\": 26.36,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": 0\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"26-Jun-2009\",\n",
    "        \"open\": 27.09,\n",
    "        \"high\": 27.22,\n",
    "        \"low\": 25.76,\n",
    "        \"close\": 25.93,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": 0\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"29-Jun-2009\",\n",
    "        \"open\": 25.93,\n",
    "        \"high\": 27.18,\n",
    "        \"low\": 25.29,\n",
    "        \"close\": 25.35,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": 5.26315789473684\n",
    "      },\n",
    "      {\n",
    "        \"date\": \"30-Jun-2009\",\n",
    "        \"open\": 25.36,\n",
    "        \"high\": 27.38,\n",
    "        \"low\": 25.02,\n",
    "        \"close\": 26.35,\n",
    "        \"signal\": \"long\",\n",
    "        \"ret\": 6.73758865248228\n",
    "      }\n",
    "    ]\n",
    ")\n",
    "open_close_color = alt.condition(\"datum.open < datum.close\",\n",
    "                                 alt.value(\"#06982d\"),\n",
    "                                 alt.value(\"#ae1325\"))\n",
    "\n",
    "rule = alt.Chart(df).mark_rule().encode(\n",
    "    alt.X(\n",
    "        'date:T',\n",
    "        timeUnit='yearmonthdate',\n",
    "        scale=alt.Scale(domain=[{\"month\": 5, \"date\": 31, \"year\": 2009},\n",
    "                                {\"month\": 7, \"date\": 1, \"year\": 2009}]),\n",
    "        axis=alt.Axis(format='%m/%d', title='Date in 2009')\n",
    "    ),\n",
    "    alt.Y(\n",
    "        'low',\n",
    "        scale=alt.Scale(zero=False),\n",
    "        axis=alt.Axis(title='Price')\n",
    "    ),\n",
    "    alt.Y2('high'),\n",
    "    color=open_close_color\n",
    ")\n",
    "\n",
    "bar = alt.Chart(df).mark_bar().encode(\n",
    "    alt.X('date:T', timeUnit='yearmonthdate'),\n",
    "    y='open',\n",
    "    y2='close',\n",
    "    color=open_close_color\n",
    ")\n",
    "\n",
    "rule + bar\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
